package cn.aitstone.org.controller;

import cn.aitstone.basic.annotation.PreAuthorize;
import cn.aitstone.basic.exception.BusinessException;
import cn.aitstone.basic.utils.AjaxResult;
import cn.aitstone.basic.utils.ExcelUtil;
import cn.aitstone.basic.utils.PageList;
import cn.aitstone.org.domain.Shop;
import cn.aitstone.org.domain.ShopAuditLog;
import cn.aitstone.org.query.ShopQuery;
import cn.aitstone.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @Description: 店铺接口类
 * @Author: Sheldon
 * @Version: 1.0
 * @Date:  2022/7/2 15:08
 */
@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "保存修改店铺", sn="shop:addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * @Title: delete
    * @Description: 删除接口
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/2 19:37
    * @Parameters: [id]
    * @Return cn.aitstone.basic.utils.AjaxResult
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "根据id删除店铺", sn="shop:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    /**
    * @Title: get
    * @Description: 根据id获取店铺信息
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/2 19:35
    * @Parameters: [id]
    * @Return cn.aitstone.org.domain.Shop
    */
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.loadById(id);
    }


    /**
    * @Title: list
    * @Description: 获取所有店铺信息
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/2 19:36
    * @Parameters: []
    * @Return java.util.List<cn.aitstone.org.domain.Shop>
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @PreAuthorize(name = "获取店铺列表", sn="shop:list")
    public PageList<Shop> json(@RequestBody ShopQuery shopQuery)
    {
        return shopService.queryData(shopQuery);
    }

    /**
    * @Title: settlement
    * @Description: TODO
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/1 15:06
    * @Parameters: []
    * @Return cn.aitstone.basic.utils.AjaxResult
    */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){

        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试");
        }
    }


    /**
    * @Title: activate
    * @Description: 激活接口
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/5 14:14
    * @Parameters: [id]
    * @Return java.lang.String
    */
    @GetMapping("/activation/{id}")
    public String activate(@PathVariable("id") Long id)
    {
        return shopService.activate(id);
    }

    /**
    * @Title: reject
    * @Description: 审核驳回接口
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/2 19:34
    * @Parameters: [shopAuditLog]
    * @Return cn.aitstone.basic.utils.AjaxResult
    */
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog){

        try {
            shopService.reject(shopAuditLog);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试");
        }
    }

    /**
    * @Title: refuse
    * @Description: 审核拒绝接口
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/2 19:34
    * @Parameters: [shopAuditLog]
    * @Return cn.aitstone.basic.utils.AjaxResult
    */
    @PostMapping("/audit/refuse")
    public AjaxResult refuse(@RequestBody ShopAuditLog shopAuditLog){

        try {
            shopService.refuse(shopAuditLog);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试");
        }
    }

    /**
    * @Title: pass
    * @Description: 审核通过接口
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/2 19:33
    * @Parameters: [shopAuditLog]
    * @Return cn.aitstone.basic.utils.AjaxResult
    */
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog){

        try {
            shopService.pass(shopAuditLog);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试");
        }
    }

    /**
    * @Title: exportExcel
    * @Description: 文件导出接口
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/2 17:15
    * @Parameters: [resp]
    * @Return void
    */
    @GetMapping ("/export/excel")
    public void exportExcel(HttpServletResponse resp){

        List<Shop> shops = shopService.loadAll();
        // List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,
        //                                   HttpServletResponse response
        ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "店铺信息.xlsx", resp);

    }

    /**
    * @Title: importExcel
    * @Description: 导入Excel表格接口
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/2 19:32
    * @Parameters: [file]
    * @Return void
    */
    @PostMapping ("/import/excel")
    public void importExcel(@RequestPart("file")MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        for (Shop shop : shops) {
            shopService.save(shop);
        }
    }

    /**
    * @Title: loadShopEchartsData
    * @Description: 统计店铺信息接口
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/2 19:33
    * @Parameters: []
    * @Return java.util.Map<java.lang.String,java.lang.Object>
    */
    @GetMapping ("/echarts")
    public Map<String, Object> loadShopEchartsData(){
        return shopService.loadShopEchartsData();

    }

    /**
    * @Title: batch
    * @Description: 批量删除
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/4 21:09
    * @Parameters: [ids]
    * @Return cn.aitstone.basic.utils.AjaxResult
    */
    @PatchMapping
    public AjaxResult batch(@RequestBody Long[] ids){
        try {
            shopService.batchdel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试");
        }

    }
}
